package dav.mobile.garuda.states.game.objects;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import dav.mobile.garuda.components.Constants;
import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.states.game.PhysicsObject;

public class Floor extends PhysicsObject {	
	//Length in world (meter)
	int	m_Length;

	//Place attributes
	int m_Place;
	float m_Height;
	
	public Floor(int place, int length, World m_World) {
		//Super, floor always start in the ground
		super(0, 0, ResourceManager.m_GameBackgroundFloors[place], m_World);
		
		//Set length
		m_Length = length;
		
		//Set place
		m_Place = place;
		
		//Create physics body
		CreatePhysicsBody(m_World);
	}

	@Override
	public void Update(float deltatime) {
		//update floor
	}

	@Override
	public void Render(SpriteBatch m_Spritebatch) {
		//draw floor
		m_Spritebatch.draw(m_Texture, getPosX(), getPosY());
	}
	
	public float getFloorHeight() {return m_Height;}
	
	//Draw looping floor according x position
	public void Render(SpriteBatch m_Spritebatch, float x) {
		//draw floor
		m_Spritebatch.draw(m_Texture, getPosX(), getPosY());
		int curr_bg = (int) x / m_Texture.getRegionWidth();
		int curr_bg_x = curr_bg * m_Texture.getRegionWidth();
		
		m_Spritebatch.draw(m_Texture, curr_bg_x, 0);
		if (x > (curr_bg_x + m_Texture.getRegionWidth() - Constants.GAME_WIDTH)) {
			m_Spritebatch.draw(m_Texture, curr_bg_x + m_Texture.getRegionWidth(), 0);
		} else if ((curr_bg!=0) && (x < (curr_bg_x + Constants.GAME_WIDTH))) {
			m_Spritebatch.draw(m_Texture, curr_bg_x - m_Texture.getRegionWidth(), 0);
		}
	}

	//Create floor static body
	@Override
	protected void CreatePhysicsBody(World m_World) {
		//Create body definition
		float height = (float) m_Texture.getRegionHeight();
		height /= Constants.PIXELS_PER_METER;
		
		//Set height
		if (m_Place==Constants.GAME_PLACE_EPILOG||m_Place==Constants.GAME_PLACE_PROLOG) {
			height-=1.6f;
		} else if (m_Place==Constants.GAME_PLACE_PRAMBANAN) {
			height-=0.5f;
		} else if (m_Place==Constants.GAME_PLACE_BUNAKEN) {
			height-=1.6f;
		} else if (m_Place==Constants.GAME_PLACE_TOBA) {
			height-=1f;
		}
		
		m_Height = height;
		
		PolygonShape m_GroundShape = new PolygonShape();
		m_GroundShape.setAsBox((float)m_Length*2.0f, m_Height);
		BodyDef m_GroundDef = new BodyDef();
		m_GroundDef.type = BodyType.StaticBody;
		m_GroundDef.position.set(getPosX() / Constants.PIXELS_PER_METER, -(Constants.GAME_HEIGHT / Constants.PIXELS_PER_METER) + 2.5f * height);
		
		//Create body
		m_Body = m_World.createBody(m_GroundDef);
		
		//Create fixture
		FixtureDef m_Fixture = new FixtureDef();
		m_Fixture.shape = m_GroundShape;
		m_Fixture.filter.groupIndex = 0;
		m_Fixture.friction = 0.75f;
		m_Body.createFixture(m_Fixture);

		//Dispose shape
		m_GroundShape.dispose();
	}

}
